#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
class A
{
public:
    bool operator()(int x, int y)
    {
        return x > y;
    }
};
int main()
{
    string s;
    while (getline(cin, s))
    {
        map<char, int> m;
        for (auto a : s)
        {
            m[a]++;
        }
        priority_queue<int, vector<int>, A> pq;
        for (auto a : m)
        {
            pq.push(a.second);
        }
        int count = 0;
        while (pq.size() > 1)
        {
            int x = pq.top();
            pq.pop();
            int y = pq.top();
            pq.pop();
            count += (x + y);
            pq.push(x + y);
        }
        cout << count << endl;
    }
}
